﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class Battle : MonoBehaviour {
    public DemoPlayer player;
    public DemoPlayer computer;

    public Button BtnReStart;

    public Text TextLeft;
    public Text TextRight;

    public float waitSeconds = 2.0f;

    public bool isWaitAction = true;

    public bool isEnd = false;

    private Dictionary<ActionType, string> _StringDict = new Dictionary<ActionType, string>();
    private Dictionary<ActionType, Color> _ColorDict = new Dictionary<ActionType, Color>();
    private int[] _mpValueArray;
    private int[] _hpValueArray;
    private int[] _defenceHpValueArray;

    private void Start() {
        BtnReStart.onClick.AddListener(ReStart);
        _StringDict[ActionType.power] = "Power";
        _StringDict[ActionType.attack] = "Bo";
        _StringDict[ActionType.defence] = "Defense";
        _StringDict[ActionType.skill] = "BigBo!";
        _ColorDict[ActionType.power] = Color.yellow;
        _ColorDict[ActionType.attack] = Color.red;
        _ColorDict[ActionType.defence] = Color.blue;
        _ColorDict[ActionType.skill] = new Color(255f, 0f, 255f);

        _mpValueArray = new int[4] { 1, -1, 0, -3 };
        _hpValueArray = new int[4] { 0, -1, 0, -3 };
        _defenceHpValueArray = new int[4] { 0, 0, 0, -1 };
        ResetText();
    }

    private DateTime LastTime;

    // Update is called once per frame
    void Update () {
        if (isEnd)
            return;
		if(player.isReady && computer.isReady) {
            isWaitAction = false;
            TextLeft.text = _StringDict[player.actionType];
            TextLeft.color = _ColorDict[player.actionType];
            TextRight.text = _StringDict[computer.actionType];
            TextRight.color = _ColorDict[computer.actionType];
            LastTime = DateTime.Now;
            player.isReady = false;
            computer.isReady = false;
            DoBattle(player, computer);
            DoBattle(computer, player);            
        }
        if(!isWaitAction && (DateTime.Now - LastTime).Seconds >= waitSeconds) {
            ResetText();
            Judgment();
        }
	}

    private void DoBattle(DemoPlayer roleA, DemoPlayer roleB) {
        var useHpArray = roleB.actionType == ActionType.defence ? _defenceHpValueArray : _hpValueArray;
        int index = (int)roleA.actionType - 1;
        roleA.mp += _mpValueArray[index];
        roleB.hp += useHpArray[index];
    }

    private void Judgment() {
        if(player.hp > 0 && computer.hp <= 0) {
            TextLeft.text = "Win";
            TextLeft.color = Color.red;
            TextRight.text = "Defeat";
            TextRight.color = Color.blue;
            End();            
        }
        else if(player.hp <= 0 && computer.hp > 0){
            TextLeft.text = "Defeat";
            TextLeft.color = Color.blue;
            TextRight.text = "Win";
            TextRight.color = Color.red;
            End();
        }
        else if (player.hp <= 0 && computer.hp <= 0) {
            TextLeft.text = "Draw";
            TextLeft.color = Color.black;
            TextRight.text = "Draw";
            TextRight.color = Color.black;
            End();
        }

    }

    private void End() {
        isEnd = true;
        BtnReStart.gameObject.SetActive(true);
    }

    private void ResetText() {
        TextRight.text = "";
        TextLeft.text = "";
        isWaitAction = true;
    }

    private void ReStart() {
        isEnd = false;
        BtnReStart.gameObject.SetActive(false);
        player.hp = 5;
        player.mp = 2;
        computer.hp = 5;
        computer.mp = 2;
        player.isReady = false;
        computer.isReady = false;
        ResetText();
    }
}
